using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using sdm.exam.domain;
using sdm.exam.domain.Abstract;
using sdm.exam.domain.DataContext;
using sdm.exam.domain.Entities;

namespace sdm.exam.test.Domain
{
    [TestClass]
    public class TeacherDomainTest
    {
        private TeacherDomain _domain = null;

        /// <summary>
        /// Initialize the test.
        /// </summary>
        [TestInitialize]
        public void Start()
        {
            Ninjector.Kernel.Bind<ISdmExamRepository>().ToConstant(new DataContext(ConnectionStrings.TestConnection));
            _domain = new TeacherDomain();
        }

        /// <summary>
        /// Cleanup the test.
        /// </summary>
        [TestCleanup]
        public void End()
        {
            Ninjector.Kernel.Unbind<ISdmExamRepository>();
        }

        [TestMethod]
        public void AddTeacherToSubjectOfSemesterClassTest()
        {
            //Ninjector.Kernel.Bind<ISdmExamRepository>().ToConstant(new DataContext(ConnectionStrings.TestConnection));
            //var domain = new sdm.exam.domain.DataContext.TeacherDomain();

            var edu = new Education(){Name = "Computer Science"};
            var startTime = new DateTime(2010, 9, 2);
            var endTime = new DateTime(2013, 2, 28);

            var teacher = new Teacher();
            var course = new Course {Name = "SDM"};
            teacher.Courses = new List<Course> {new Course() {Name = "SDM"}};
            var semesterClass = new SemesterClass()
                                    {Education = edu, EndDate = endTime, StartDate = startTime, Name = "2010"};

            bool sucess = _domain.AddTeacherToSubjectOfSemesterClass(teacher, course, semesterClass);

            Assert.IsTrue(sucess);
        }

        /// <summary>
        /// Checks whether or not we are getting a of courses which have been connected to a teacher.
        /// </summary>
        [TestMethod]
        public void GetListOfAllCourseConenctedToTeacherTest()
        {
            const int teacherCpr = 0123456789;

            //Ninjector.Kernel.Bind<ISdmExamRepository>().ToConstant(new DataContext(ConnectionStrings.TestConnection));
            //var domain = new TeacherDomain();

            var t = new Teacher();
            var c = new Course();

            t.Cpr = teacherCpr;
            t.Courses = new List<Course>
                            {
                                new Course() {Name = "SDM"},
                                new Course() {Name = "Android"},
                                new Course() {Name = "SharePoint"}
                            };

            //domain.AddTeacherToSubjectOfSemesterClass(t);

            List<TeacherCourse> tcList = _domain.GetListOfAllCourseConnectedToTeacher();
            Assert.IsNotNull(tcList);
        }

        /// <summary>
        /// Checks whether or not the teacher is allowed to teach in the course he has been assigned to.
        /// </summary>
        [TestMethod]
        public void TeacherHasCourseTest() //WORKS
        {
            //Ninjector.Kernel.Bind<ISdmExamRepository>().ToConstant(new DataContext(ConnectionStrings.TestConnection));
            //var domain = new TeacherDomain();

            var teacher = new Teacher();
            var course = new Course();

            try
            {
                var c = new Course() { Name = "SDM" };
                teacher.Courses = new List<Course> {c};
            }
            catch (NotSupportedException e)
            {
                Assert.Fail();
            }
            course.Name = "SDM";

            bool hasCourse = _domain.TeacherHasCourse(course, teacher.Courses);

            Assert.AreEqual(true, hasCourse);
        }

        /// <summary>
        /// Adds a teacher to the database.
        /// </summary>
        [TestMethod]
        public void AddTeacherTest()
        {
            //Ninjector.Kernel.Bind<ISdmExamRepository>().ToConstant(new DataContext(ConnectionStrings.TestConnection));
            //var domain = new TeacherDomain();

            var teacher = new Teacher {Courses = new List<Course> {new Course {Name = "SDM"}}};
            bool sucess = _domain.AddTeacher(teacher);

            Assert.IsTrue(sucess);
        }
    }
}
